// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include "CameraWidget.h"

CameraWidget::CameraWidget(Camera *camera, QWidget *parent) :
    CollapsibleWidget(parent),
    m_camera(camera),
    m_fov(new QDoubleSpinBox()),
    m_aspectRatio(new QDoubleSpinBox()),
    m_near(new QDoubleSpinBox()),
    m_far(new QDoubleSpinBox())
{    
    m_fov->setRange(1, 180);
    m_aspectRatio->setRange(0, 100);
    m_near->setRange(0, 10);
    m_far->setRange(0, 10000);
    setTitle("Camera");

    QWidget* w = new QWidget();
    QFormLayout *l = new QFormLayout(w);
    l->addRow(new QLabel("Field of View"), m_fov);
    l->addRow(new QLabel("Aspect Ratio"), m_aspectRatio);
    l->addRow(new QLabel("Near Limit"), m_near);
    l->addRow(new QLabel("Far Limit"), m_far);

    setWidget(w);
    onCameraChanged();

    connect(m_fov, SIGNAL(valueChanged(double)), this, SLOT(onChanged()));
    connect(m_aspectRatio, SIGNAL(valueChanged(double)), this, SLOT(onChanged()));
    connect(m_near, SIGNAL(valueChanged(double)), this, SLOT(onChanged()));
    connect(m_far, SIGNAL(valueChanged(double)), this, SLOT(onChanged()));
    connect (m_camera, SIGNAL(changed()), this, SLOT(onCameraChanged()));
}

void CameraWidget::onChanged()
{
    disconnect(m_camera, 0, this, 0);
    m_camera->setFieldOfView(m_fov->value());
    m_camera->setAspectRatio(m_aspectRatio->value());
    m_camera->setNearLimit(m_near->value());
    m_camera->setFarLimit(m_far->value());
    connect (m_camera, SIGNAL(changed()), this, SLOT(onCameraChanged()));
    emit cameraChanged();
}

void CameraWidget::onCameraChanged()
{
    m_fov->setValue(m_camera->getFieldOfView());
    m_aspectRatio->setValue(m_camera->getAspectRatio());
    m_near->setValue(m_camera->getNearLimit());
    m_far->setValue(m_camera->getFarLimit());

    m_near->setRange(0, m_far->value());
    m_far->setRange(m_near->value(), 10000);
    emit cameraChanged();
}

